package com.karolak.subject;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.bushe.swing.event.EventBus;

import com.karolak.event.EmailMessage;
import com.karolak.event.MailMessage;
import com.karolak.observer.EmailReader;
import com.karolak.observer.MailReader;

public class Publisher {

	public static void main(String[] args) throws InterruptedException {
		run();
	}

	public static void run() {

		final List<Object> list = new ArrayList<Object>();

		for (int i = 1; i < 2; i++) {
			list.add(new EmailReader(String.valueOf(i)));
			list.add(new MailReader(String.valueOf(i)));
		}

		JFrame frame = new JFrame() {
			private static final long serialVersionUID = 1L;
			@SuppressWarnings("unused")
			List<Object> readers = list;
		};
		JButton mailButton = new JButton("Send Mail");
		final JTextField mailField = new JTextField();
		mailField.setPreferredSize(new Dimension(300, 30));

		JButton emailButton = new JButton("Send Email");
		final JTextField emailField = new JTextField();
		emailField.setPreferredSize(new Dimension(300, 30));

		frame.add(mailButton);
		frame.add(mailField);
		frame.add(emailButton);
		frame.add(emailField);
		frame.setLayout(new GridLayout(2, 2));
		frame.setSize(500, 100);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		mailButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String msg = mailField.getText();
				EventBus.publish(new MailMessage(msg));
			}
		});
		emailButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String msg = emailField.getText();
				EventBus.publish(new EmailMessage(msg));
			}
		});
	}
}
